/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import entity.Feedback;
import entity.UserInfo;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import util.LogUtil;

/**
 *
 * @author VietTien
 */
@Stateless
public class UserInfoFacade extends AbstractFacade<UserInfo> {

    @PersistenceContext(unitName = "BluePumpkin-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public UserInfoFacade() {
        super(UserInfo.class);
    }

    public UserInfo checkLogin(String user, String pass) {
        try {
            String sql = "SELECT u FROM UserInfo u WHERE u.userID = :userID AND u.userPass = :userPass OR u.email = :email AND u.userPass = :userPass";
            Query query = em.createQuery(sql);
            query.setParameter("userID", user);
            query.setParameter("email", user);
            query.setParameter("userPass", pass);
            UserInfo u = (UserInfo) query.getSingleResult();
            if (u != null) {
                return u;
            } else {
                return null;
            }

        } catch (NoResultException e) {
            return null;
        }

    }

    public List<Feedback> findAllFeedBack(UserInfo user) {
        Query query = em.createQuery("SELECT a FROM Feedback a WHERE a.userInfo  = :userInfo");
        query.setParameter("userInfo", user);
        return query.getResultList();
    }

    public List<UserInfo> listUser() {
        Query query = em.createQuery("SELECT a FROM UserInfo a WHERE a.isAdmin  = :isAdmin");
        query.setParameter("isAdmin", Boolean.FALSE);
        return query.getResultList();
    }
    public String CheckUserID(String ID) {
        UserInfo ev = new UserInfo(); // gọi Entity
        ev = null; // xóa trông bộ nhó đệm
        ev = em.find(UserInfo.class, ID); // tìm ID
        if (ev !=null) {
            return ev.getUserID(); // trả về khi ID tồn tại
        }
        return null; // ID ko tồn tại
    }
    public boolean CheckEmail(String email) {
        Query query=em.createQuery("SELECT u FROM UserInfo u WHERE u.email = :email");
        query.setParameter("email",email);
        List resultList = query.getResultList();
        if(resultList.size()>0)
        {
            return true;
        }else{
        return false;
        }
    }
    public List<UserInfo> getUserByEventID(String id) {
        String sql = "select u.[UserID] as 'USERID' ,u.[UserPass] ,u.[UserName],u.[Gender],u.[BirthDay],u.[Email],u.[AddressInfo],u.[PhoneNumber],u.[IsAdmin],u.[IsFullControl],u.[isDelete] from  UserInfo  u inner join Registration r on r.UserID = u.UserID inner join EventInfo e on e.EventID = r.EventID Where e.EventID='" +  id + "'  AND u.isDelete=0";
        List<UserInfo> result = null;
        try {
            result= em.createNativeQuery(sql,UserInfo.class).getResultList();
        } catch (Exception e) {
            LogUtil.doLog(e);
        }
        return result;
    }
}
